package com.kehutong.api.AccessControlAction;

import com.kehutong.auth.entity.Account;
import com.kehutong.auth.entity.User;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import org.coraframework.util.secret.MD5;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
/**
 * 更具open_id 和access_tokken 换去用户信息
 * @author: liul
 * @date: 2021-06-11 14:10
 */
@MvcAction
@ReqMapping("/oau-kht/user")
public class UserAction {

    private static final Logger logger = LoggerFactory.getLogger(UserAction.class);
    @Inject
    private JdbcSession jdbcSession;
    @ReqMapping("/getUser")
    public Object getUser(JSONObject params) throws Exception {

        String access_token = params.getString("access_token");
        logger.info("access_token1111:" + access_token);
        String open_id = params.getString("open_id");
        logger.info("open_id1111:" + open_id);
        String sign = params.getString("sign");
        Long timestamp =params.getLong("timestamp");
        if(Objects.isEmpty(access_token) && Objects.isEmpty(open_id)){
            throw new RuntimeException("参数不存在");
        }
        //字符串拼接
        String src=open_id+access_token+timestamp+"c857bcee4dbe7cc94a668e5f0f21c216";
        String code =  MD5.MD5Encode(src);
        logger.info("加密后:" + code);
        JSONObject jsonObject = new JSONObject();
        if(sign.equals(code)){
            //获取用户信息
            User user = jdbcSession.findById(User.class, open_id);

            if (Objects.nonNull(user) && user.isDeleted()) {
                throw new RuntimeException("用户信息不存在");
            }
          /*   if(user.getRoles().contains("5") ||user.getRoles().contains("4")){*/
                 jsonObject.put("role_type", 1);
            /* }else{
                 jsonObject.put("role_type", 99);
             }*/
            jsonObject.put("name", user.getName());
            jsonObject.put("phone",user.getMobile());
        }else{
            throw new RuntimeException("非法请求");
        }
        return Result.text(jsonObject.toJSONString());
    }
    @ReqMapping("/getWeChatUser")
    public Object getWeChatUser(JSONObject params) throws Exception {
        System.out.println("进来:" );
        String access_token = params.getString("access_token");
        String open_id = params.getString("open_id");
        String sign = params.getString("sign");
        Long timestamp =params.getLong("timestamp");
            if(Objects.isEmpty(access_token) && Objects.isEmpty(open_id)){
                throw new RuntimeException("参数不存在");
            }
            //字符串拼接
            String src=open_id+access_token+timestamp+"c857bcee4dbe7cc94a668e5f0f21c216";
            String code =  MD5.MD5Encode(src);
            logger.info("加密后:" + code);
            JSONObject jsonObject = new JSONObject();
            if(sign.equals(code)){
                //获取用户信息
                Account user = jdbcSession.findById(Account.class, open_id);
                if (Objects.isNull(user) ) {
                    throw new RuntimeException("用户信息不存在");
                }
                jsonObject.put("role_type", 1);
                jsonObject.put("name", user.getRealname());
                jsonObject.put("phone",user.getMobile());

            }else{
            throw new RuntimeException("非法请求");
        }
        return Result.text(jsonObject.toJSONString());
    }


}
